define(['require' , 'exports' , 'module' , 'jquery' , 'layer' , 'common/common' , 'baiduTemplate' , 'dropload'] , function (require , exports , module , $ , layer , common) {

    var dropload = function() {
        // 上拉加载-----start------
        dropload = $('#content').dropload({
            distance : 100,
            domDown : {
                domClass   : 'dropload-down',
                domRefresh : '<div class="dropload-refresh">↑上拉加载更多</div>',
                domLoad    : '<div class="dropload-load"><span class="loading"></span>加载中...</div>',
                domNoData  : '<div class="dropload-noData">暂无数据</div>'
            },
            loadDownFn : function(load){
                var page = window.sessionStorage.getItem('goodspage');
                if(page == undefined || page == null){
                    page = 1;
                }

                var nav = $('#current-nav').val();
                //这里的sessionStorage是为了解决微信内置浏览器返回上一页强制刷新的问题

                var lngLat = common.getLngLat();
                $.ajax({
                    type: 'GET',
                    url: '/goods/list?page=' + page + '&lng=' + lngLat.lng + '&lat=' + lngLat.lat + '&nav=' + nav,
                    dataType: 'json',
                    success: function(data){
                        if(data.code == 0) {
                            if (data.data.list.length == 0) {
                                load.noData(true);
                                load.resetload();
                            }else {
                                window.sessionStorage.setItem('goodspage' , data.data.pageData.page * 1 + 1)
                                var uList = $('.list');
                                var boxLen = uList.find('li').length;

                                //更新商品的购买数量
                                for (var list of data.data.list) {
                                    for (var cart of data.data.cartList) {
                                        if (list.id == cart.g_id) {
                                            list.buy_num = cart.buy_num;
                                        }
                                    }
                                }
                                var html = baidu.template('goodsList', {data: data.data.list, len: boxLen});
                                uList.append(html);
                                load.resetload();
                            }
                        }else{
                            load.noData(true);
                            load.resetload();
                        }
                    },
                    error: function(xhr, type){
                        layer.open({
                            content : '服务器链接错误' ,
                            skin : 'msg' ,
                            time : 2
                        });
                        // 即使加载出错，也得重置
                        load.noData(true);
                        load.resetload();
                    }
                });

            }
        });
        return dropload;
    };

    var storageGoods = function () {
        $('#content').on('click' , '.list a' , function () {
            /**
             * 微信内置浏览器返回上一页强制刷新
             */
            var storageHtml = $('.list').html();
            sessionStorage.setItem("storageHtml", storageHtml);
            sessionStorage.setItem("top",document.body.scrollTop);

        });
    };

    var showNav = function () {
      $('#nav-button').bind('click' , function () {
          var _nav = $('#nav');
          var time = 500;
          if(_nav.hasClass('show')){
              _nav.removeClass('show').animate({
                  left: -_nav.width()
              } , time);
              $('#content').animate({
                  left : 0
              } , time);
          }else {
              $('#content').animate({
                  left : _nav.width()
              } , time);
              _nav.addClass('show').animate({
                  left: 0
              } , time);
          }
      })
    };

    var getNavList = function () {
        $.ajax({
            type: 'GET',
            url: '/nav/list',
            dataType: 'json',
            success: function(data){
                var nav = $('#current-nav').val();
                if(nav == 0 || nav == ''){
                    var html = "<ul><li class='current-nav-show'><a href='/?nav=0' class='current-nav-show'>所有产品</a></li>";
                }else{
                    var html = "<ul><li><a href='/?nav=0'>所有产品</a></li>";
                }

                if(data.code == 0) {
                    for (var list of data.data) {
                        if(nav == list.id){
                            html += "<li class='current-nav-show'><a href='/?nav=" + list.id +"'>"+ list.name +"</a></li>";
                        }else{
                            html += "<li><a href='/?nav=" + list.id +"'>"+ list.name +"</a></li>";
                        }

                    }
                }else{
                    layer.open({
                        content : data.msg ,
                        skin : 'msg' ,
                        time : 2
                    });
                }
                html += "</ul>";

                $('#nav').html(html);
            },
            error: function(xhr, type){
                layer.open({
                    content : '服务器链接错误' ,
                    skin : 'msg' ,
                    time : 2
                });
            }
        });

    };

    exports.getNavList = getNavList;

    exports.showNav = showNav;
    exports.storageGoods = storageGoods;
    exports.dropload = dropload;
});
